﻿using System;
using System.Collections.Generic;
using System.Data;
using Loki.Data;

namespace Luna.Data.Budget
{
    /// <summary>
    /// Mapper for data interface IBudgetData.
    /// </summary>
    public partial class BudgetDataMapper : LunaMapper<BudgetData, IBudgetData>
    {
        public BudgetDataMapper()
        {
            GetAllRequest = "Budget.budget_select_all";
            GetRequest = "Budget.budget_select";
            InsertRequest = "Budget.budget_insert";
            UpdateRequest = "Budget.budget_update";
            DeleteRequest = "Budget.budget_delete";
            GetAllByMainBudgetIDRequest = "Budget.budget_select_by_mainbudgetid";
        }

        private const string MainBudgetIDColumnName = "MainBudgetID";
        private const string NameColumnName = "Name";
        private const string StartColumnName = "Start";
        private const string EndColumnName = "End";
        private const string CommentColumnName = "Comment";
        private const string LastVersionColumnName = "LastVersion";
        private const string ValidatedColumnName = "Validated";

        public string GetAllByMainBudgetIDRequest { get; set; }

        protected override void LoadCoreData(IBudgetData entity, IDataRecord record)
        {
            entity.MainBudgetID = record.GetGuid(MainBudgetIDColumnName);
            entity.Name = record.GetString(NameColumnName);
            entity.Start = record.GetDate(StartColumnName);
            entity.End = record.GetNullableDate(EndColumnName);
            entity.Comment = record.GetString(CommentColumnName);
            entity.LastVersion = record.GetBoolean(LastVersionColumnName);
            entity.Validated = record.GetBoolean(ValidatedColumnName);
        }

        protected override void MapSaveParameters(IDatabaseCommand command, IBudgetData entity)
        {
            command.AddParameter(MainBudgetIDColumnName, entity.MainBudgetID);
            command.AddParameter(NameColumnName, entity.Name);
            command.AddParameter(StartColumnName, entity.Start);
            command.AddParameter(EndColumnName, entity.End);
            command.AddParameter(CommentColumnName, entity.Comment);
            command.AddParameter(LastVersionColumnName, entity.LastVersion);
            command.AddParameter(ValidatedColumnName, entity.Validated);
            command.AddParameter(IDParameterName, entity.PK);
        }

        public IEnumerable<IBudgetData> GetAllByMainBudgetID(IDbTransaction transaction, Guid mainBudgetID)
        {
            return ExecuteGetAll(
                    transaction,
                    GetAllByMainBudgetIDRequest,
                    c =>
                    {
                        c.AddParameter(MainBudgetIDColumnName, mainBudgetID);
                    });
        }
    }
}